package com.xxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxx.dao.AddressDao;
import com.xxx.dao.DistrictDao;
import com.xxx.pojo.Address;
import com.xxx.pojo.District;
import com.xxx.service.AddressService;
import com.xxx.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Autowired
    private DistrictDao districtDao;

    @Value("${user.address.max_count}")
    private Integer MAX_COUNT;

    //新增收货地址功能
    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        Map<String, Object> map=new HashMap<>();
        map.put("uid",uid);
        List<Address> list = addressDao.selectByMap(map);
        if(list.size()>=10){
            throw new AddressCountLimitException("(新增收货地址时)用户收货地址超出上限");
        }
        // 设置用户uid 是否为默认地址is_default
        address.setUid(uid);
        Integer isDefault=list.size()==0?1:0;//1 表示设为默认 0表示否
        address.setIsDefault(isDefault);
        //补全数据
        address.setCreatedUser(username);
        address.setModifiedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date());

        //通过code获取省市县名称
        //获取省名 存储
        map.clear();
        String provinceCode=address.getProvinceCode().trim();
        map.put("code",provinceCode);
        List<District> res = districtDao.selectByMap(map);
        address.setProvinceName(res.get(0).getName());
        //获取市名 存储
        map.clear();
        String cityCode=address.getCityCode().trim();
        map.put("code",cityCode);
        res = districtDao.selectByMap(map);
        address.setCityName(res.get(0).getName());
        //获取县名 存储
        map.clear();
        String areaCode=address.getAreaCode().trim();
        System.out.println("=========>"+areaCode);
        map.put("code",areaCode);
        res = districtDao.selectByMap(map);
        address.setAreaName(res.get(0).getName());

        //执行插入地址操作
        int row = addressDao.insert(address);
        if(row!=1){
            throw new InsertException("(新增收货地址时)插入用户地址时产生未知异常");
        }
    }

    //显示收货地址功能
    @Override
    public List<Address> getByUid(Integer uid) {
        LambdaQueryWrapper<Address> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUid,uid).orderByDesc(Address::getIsDefault);
        List<Address> list = addressDao.selectList(wrapper);
        return list;
    }

    //设置默认收货地址功能
    @Override
    public void setDefaultAddress(Integer aid, Integer uid, String username) {
        //判断收货地址是否存在 防止在别的地方被删除
        Map<String, Object> map=new HashMap<>();
        map.put("aid",aid);
        List<Address> result = addressDao.selectByMap(map);
        if(result.size()==0){
            throw new AddressNotFoundException("(设置默认收货地址时)收货地址不存在");
        }
        //把uid下的所有收货地址is_default设置为0 非默认状态
        LambdaQueryWrapper<Address> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Address::getUid,uid);
        Address address=new Address();
        address.setIsDefault(0);
        int row = addressDao.update(address, wrapper);
        if(row==0){
            throw new UpdatException("(设置所有收货地址默认状态为0时)更新出现异常");
        }
        //设置新选中的地址为默认
        wrapper.clear();
        wrapper.eq(Address::getAid,aid);
        address.setIsDefault(1);
        int row2=addressDao.update(address,wrapper);
        if(row2==0){
            throw new UpdatException("(设置默认地址时)更新出现异常");
        }
    }

    //删除收货地址功能
    @Override
    public void deleteAddress(Integer aid,Integer uid) {
        //先查询收货地址是否存在
        //判断收货地址是否存在 防止在别的地方被删除
        Map<String, Object> map=new HashMap<>();
        map.put("aid",aid);
        map.put("uid",uid);
        List<Address> result = addressDao.selectByMap(map);
        if(result.size()==0){
            throw new AddressNotFoundException("(删除收货地址操作时)收货地址不存在");
        }
        //执行删除操作
        map.clear();
        map.put("aid",aid);
        int row = addressDao.deleteByMap(map);
        if(row==0){
            throw new DeleteException("(删除收货地址操作时)删除收货地址操作出现异常");
        }
        //删除后重新查询把降序排列第一条数据is_default设为1默认地址
        List<Address> list = this.getByUid(uid);
        if (list.size()!=0){
            list.get(0).setIsDefault(1);
            LambdaQueryWrapper<Address> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Address::getAid,list.get(0).getAid());
            addressDao.update(list.get(0),wrapper);
        }
    }

    //回显要修改收货地址的数据
    @Override
    public Address getOneByAid(Integer aid) {
        Map<String, Object> map=new HashMap<>();
        map.put("aid",aid);
        List<Address> result = addressDao.selectByMap(map);
        if(result.size()==0){
            throw new AddressNotFoundException("(回显单个收货地址时)收货地址不存在");
        }
        return result.get(0);
    }
    //修改地址功能
    @Override
    public void updateAddress(Address address,Integer uid,String username) {
        //补全数据
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());

        //通过code获取省市县名称
        //获取省名 存储
        Map<String, Object> map=new HashMap<>();
        String provinceCode=address.getProvinceCode().trim();
        map.put("code",provinceCode);
        List<District> res = districtDao.selectByMap(map);
        address.setProvinceName(res.get(0).getName());
        //获取市名 存储
        map.clear();
        String cityCode=address.getCityCode().trim();
        map.put("code",cityCode);
        res = districtDao.selectByMap(map);
        address.setCityName(res.get(0).getName());
        //获取县名 存储
        map.clear();
        String areaCode=address.getAreaCode().trim();
        System.out.println("=========>"+areaCode);
        map.put("code",areaCode);
        res = districtDao.selectByMap(map);
        address.setAreaName(res.get(0).getName());

        LambdaQueryWrapper<Address> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Address::getAid,address.getAid());
        int row = addressDao.update(address, wrapper);
        if(row==0){
            throw new UpdatException("(修改地址操作时)更新数据出现异常");
        }
    }
}
